package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.constant.CacheConstant;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description ResuorceController
 * @Author apeng
 * @Date 2024-10-23
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理")
public class ResuorceController {

    @Autowired
    private ResourceService resourceService;

    @ApiOperation(value = "资源列表",notes = "资源列表")
    @PostMapping("/list")
    @ApiImplicitParam(name = "resourceDto",value = "资源DTO对象",required = true,dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.parentResourceNo","resourceDto.resourceType"})
    @Cacheable(cacheNames = CacheConstant.RESOURCE_LIST,key = "#resourceDto.hashCode()")
    public ResponseResult<List<ResourceVo>> resourceList(@RequestBody ResourceDto resourceDto){

        List<ResourceVo> resourceVoList = resourceService.findResourceList(resourceDto);
        return ResponseResult.success(resourceVoList);

    }


    /**
     * @param resourceDto 资源对象
     * @return
     *  资源树形
     */
    @PostMapping("/tree")
    @ApiOperation(value = "资源树形", notes = "资源树形")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.label"})
    @Cacheable(cacheNames = CacheConstant.RESOURCE_TREE,key = "#resourceDto.hashCode()")
    public ResponseResult<TreeVo> resourceTreeVo(@RequestBody ResourceDto resourceDto) {
        TreeVo treeVo = resourceService.resourceTreeVo(resourceDto);
        return ResponseResult.success(treeVo);
    }


    @PutMapping
    @ApiOperation(value = "资源添加",notes = "资源添加")
    @ApiImplicitParam(name = "resourceDto",value = "资源Dto对象",required = true,dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"})
@Caching(evict = {
        @CacheEvict(cacheNames = CacheConstant.RESOURCE_LIST,key = "#resourceDto.hashCode()"),
        @CacheEvict(cacheNames = CacheConstant.RESOURCE_TREE,key = "#resourceDto.hashCode()")
})
    public ResponseResult createResource(@RequestBody ResourceDto resourceDto){
        return resourceService.createResource(resourceDto);
    }

    //资源修改
    @PatchMapping
    @ApiOperation("资源修改")
    @ApiImplicitParam(name = "resourceDto",value = "资源Dto对象",dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"
            ,"resourceDto.id"})
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheConstant.RESOURCE_LIST,key = "#resourceDto.hashCode()"),
            @CacheEvict(cacheNames = CacheConstant.RESOURCE_TREE,key = "#resourceDto.hashCode()")
    })
    public ResponseResult<Boolean> updateResource(@RequestBody ResourceDto resourceDto){
        return resourceService.updateResource(resourceDto);
    }



    //启用禁用
    @PostMapping("/enable")
    @ApiOperation("启用禁用")
    @ApiImplicitParam(name = "resourceDto",value = "资源Dto对象",dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.parentResourceNo"
            , "resourceDto.sortNo"})
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheConstant.RESOURCE_LIST,key = "#resourceDto.hashCode()"),
            @CacheEvict(cacheNames = CacheConstant.RESOURCE_TREE,key = "#resourceDto.hashCode()")
    })
    public ResponseResult<Boolean> startOrStop(@RequestBody ResourceDto resourceDto){
        return resourceService.startOrStop(resourceDto);
    }


    @DeleteMapping("/{resourceNo}")
    @ApiOperation("删除资源")
    @ApiImplicitParam(name = "resourceNo",value = "资源编号",dataType = "String")
    @ApiOperationSupport(includeParameters = {"resourceNo"})
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheConstant.RESOURCE_LIST,key = "#resourceDto.hashCode()"),
            @CacheEvict(cacheNames = CacheConstant.RESOURCE_TREE,key = "#resourceDto.hashCode()")
    })
    public ResponseResult deleteResource(@PathVariable String resourceNo){
        resourceService.updateByResourcNo(resourceNo);
        return ResponseResult.success();
    }

}
